package com.lutzyt.delivery.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lutzyt.delivery.bean.Order;
import com.lutzyt.delivery.bean.OrderItem;
import com.lutzyt.delivery.service.OrderItemService;
import com.lutzyt.delivery.service.OrderService;
import com.lutzyt.delivery.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * controller 控制器
 *
 * @author zyt
 * @date 2021-06-08
 */
@Slf4j
@RestController
@RequestMapping("/api/order")
public class OrderController {
  private OrderService service;
  private OrderItemService itemService;

  @GetMapping("/{id}")
  public Result<Order> getById(@PathVariable Integer id) {
    final Order bean = service.findById(id);
    return Result.success(bean);
  }

  @GetMapping("/init/list")
  public Result<List<Order>> inits(){
    List<Order> list = service.initList();
    return Result.success(list);
  }

  @GetMapping("/{id}/item")
  public Result<List<OrderItem>> getItems(@PathVariable Integer id) {
    final List<OrderItem> list = itemService.findByOrderId(id);
    return Result.success(list);
  }

  @PutMapping("/{id}/replay")
  public Result<Void> replay(@PathVariable Integer id){
    service.replay(id);
    return Result.success();
  }

  @PutMapping("/{id}/delivery")
  public Result<Void> delivery(@PathVariable Integer id){
    service.delivery(id);
    return Result.success();
  }

  @GetMapping("/{id}/item/page")
  public Result<Page<OrderItem>> getItemPage(@PathVariable Integer id,Integer pageSize, Integer pageNum) {
    final Page<OrderItem> list = itemService.pageByOrderId(id, pageSize, pageNum);
    return Result.success(list);
  }

  @PutMapping("/{id}")
  public Result<Void> update(@RequestBody Order bean) {
    log.info("更新菜单:{}", bean);
    service.update(bean);
    return Result.success();
  }

  @PostMapping("/page")
  public Result<Page<Order>> page(Integer pageSize, Integer pageNum) {
    final Page<Order> page = service.page(new Page<>(pageNum, pageSize));
    return Result.success(page);
  }

  @DeleteMapping("/{id}")
  public Result<Void> del(@PathVariable Integer id) {
    log.info("删除:{}", id);
    service.deleteById(id);
    return Result.success();
  }

  @PostMapping
  public Result<Void> insert(@RequestBody Order bean) {
    log.info("添加菜单:{}", bean);
    service.insert(bean);
    return Result.success();
  }

  @Autowired
  public void setItemService(OrderItemService itemService) {
    this.itemService = itemService;
  }

  @Autowired
  public void setService(OrderService service) {
    this.service = service;
  }
}
    